﻿using iService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySqlX.XDevAPI.Common;
using Service;
using swiftCode.Bbs.Model;
using swiftCode.Bbs.Model.Model;

namespace swiftCode.Bbs.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 所有用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet(Name = "getallUser")]
        public async Task<MessageModel<List<User>>> getallUser()
        {
            var user = await _userService.GetAllUser();
            return new MessageModel<List<User>>()
            {
                success = user.Count == 0 ? false : true,
                msg = user.Count == 0 ? "未找到符合条件的内容" : "搜索成功",
                response = user.Count == 0 ? null : user
            };
        }

        /// <summary>
        /// 用户获取个人信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet(Name = "userGetInfor")]
        public async Task<MessageModel<User>> GetInfor(int id)
        {
            User user = await _userService.getSelfInfor(d => d.UserId == id);
            if(user != null)
            {
                return new MessageModel<User>()
                {
                    success = true,
                    msg = "获取成功",
                    response = user
                };
            }
            else
            {
                return new MessageModel<User>()
                {
                    success = false,
                    msg = "获取失败",
                };
            }    
    }

        /// <summary>
        /// 通过用户名获得信息
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        [HttpGet(Name = "selectusername")]
        public async Task<MessageModel<List<User>>> selectUserByName(string username)
        {
            var user = await _userService.selectAllUser(d => d.UserName == username);
            if (user != null)
            {
                return new MessageModel<List<User>>()
                {
                    success = true,
                    msg = "获取成功",
                    response = user
                };
            }
            else
            {
                return new MessageModel<List<User>>()
                {
                    success = false,
                    msg = "获取失败",
                };
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet(Name = "login")]
        public async Task<MessageModel<User>> Login(string Username, string password)
        {
            var currentUser = await _userService.login(d => d.UserName == Username);
            if (currentUser == null || currentUser.Password != password)
            {
                return new MessageModel<User>()
                {
                    success = false,
                    msg = "用户名或密码错误"
                };
            }
            else
            {
                return new MessageModel<User>()
                {
                    success = true,
                    msg = "登陆成功",
                    response = currentUser
                };
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost(Name = "addNewUser")]
        public async Task<MessageModel<User>> AddNewUser(User user)
        {
            var existUser = await _userService.selectUser(d => d.UserName == user.UserName);
            if (existUser != null)
            {
                return new MessageModel<User>()
                {
                    success = false,
                    msg = "用户名重复"
                };
            }
            else if (user.Password.Length < 8 || user.Password.Length > 20)
            {
                return new MessageModel<User>()
                {
                    success = false,
                    msg = "密码长度至少为8最长为20"
                };
            }
            else
            {
                user.Role = 2;
                user.AvatarPath = null;
                return new MessageModel<User>()
                {
                    success = true,
                    msg = "创建成功",
                    response = await _userService.addUserInfor(user, true)
                };
            }
        }
        /// <summary>
        /// 注销账户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet(Name = "deleteUser")]
        public async Task<MessageModel<User>> DeleteUser(int id)
        {
            User user = await _userService.selectUser(d => d.UserId == id);
            if(user != null) 
            {
                await _userService.removeUserInfor(d => d.UserId == id, true);
                return new MessageModel<User>()
                {
                    success = true,
                    msg = "删除成功",
                    response = user
                };
            }
            else
            {
                return new MessageModel<User>()
                {
                    success = false,
                    msg = "删除失败"
                };
            }    
        }

        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpGet(Name = "updateUser")]
        public async Task<MessageModel<User>> UpdateUser(int userId, string Username, string password)
        {
            var existingUser = await _userService.selectUser(d => d.UserId == userId);
            var sameUser = await _userService.selectUser(d => d.UserName == Username);
            if(existingUser.UserId != sameUser.UserId) 
            {
                return new MessageModel<User>()
                {
                    success = false,
                    msg = "已存在相同用户名"
                };
            }else if(password.Length < 8 || password.Length > 20)
            {
                return new MessageModel<User>()
                {
                    success = false,
                    msg = "密码不符合规则"
                };
            }
            else
            {
                existingUser.UserName = Username;
                existingUser.Password = password;
                return new MessageModel<User>()
                {
                    success = true,
                    msg = "更新成功",
                    response = await _userService.updateUserInfor(existingUser, true)
                };
            }   
        }


        /// <summary>
        /// 管理员给普通用户授权
        /// </summary>
        /// <param name="AdminId"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet(Name = "authorization")]
        public async Task<MessageModel<String>> authorization(int AdminId, int UserId)
        {
            User admin = await _userService.selectUser(d => d.UserId ==  AdminId);
            User user = await _userService.selectUser(d => d.UserId == UserId);
            if(admin.Role == 2)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = "普通用户无授权权限"
                };
            }else if(user.Role == 1)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = user.UserName + "已经是管理员"
                };
            }
            else
            {
                user.Role = 1;
                await _userService.updateUserInfor(user, true);
                return new MessageModel<String>()
                {
                    success = true,
                    msg = "授权成功"
                };
            }
        }

        /// <summary>
        /// 管理权转让
        /// </summary>
        /// <param name="AdminId"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet(Name = "transferAuthorization")]
        public async Task<MessageModel<String>> transferAuthorization(int AdminId, int UserId)
        {
            User admin = await _userService.selectUser(d => d.UserId == AdminId);
            User user = await _userService.selectUser(d => d.UserId == UserId);
            if (admin.Role == 2)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = admin.UserName + "不是管理员"
                };
            }
            else if (user.Role == 1)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = user.UserName + "已经是管理员"
                };
            }
            else
            {
                admin.Role = 2;
                await _userService.updateUserInfor(admin, true);

                user.Role = 1;
                await _userService.updateUserInfor(user, true);
                return new MessageModel<String>()
                {
                    success = true,
                    msg = "转交成功，再见亲爱的管理员"
                };
            }
        }
    }
}
